<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
require_once(CONFIG_PATH . CONFIG_DIR_LIB . 'phpThumb_1.7.3/phpthumb.class.php');

//
function func_thumb_generate_thumb($sourceFilename, $filename = '' , $width = 500, $height = 500, $zc = 0, $aoe = true, $watermark = false) {
	if( $sourceFilename == '' ) {
		return false;
	}
	if( $filename == '' ) {
		$filename = $sourceFilename;
	}
	
	//
	if( !is_dir(dirname($filename)) ) {
		if( !mkdir(dirname($filename), 0777) ) {
			return false;
		}
	}
	
	//flash
	if( strtolower(substr($sourceFilename, -4)) == '.swf' ) {
		if( $filename != $sourceFilename ) {
			func_thumb_copy($sourceFilename, $filename);
		}
		return false;
	}
	
	// create new phpThumb() object
	$phpThumb = new phpThumb();
	
	// set data
	// $phpThumb->rawImageData = $binary_image_data;
	$phpThumb->src = $sourceFilename;
	
	// set parameters (see "URL Parameters" below)
	$phpThumb->w = $width;
	$phpThumb->h = $height;
	$phpThumb->q = 80;
	
	//
	$phpThumb->zc = ($zc > 0) ? 1 : 0;
	
	//$phpThumb->aoe = true;
	$phpThumb->aoe = ($aoe == true ? true : false);
	
	////
	//$phpThumb->setSourceFilename($sourceFilename);
	//$phpThumb->setParameter('w', $width);
	//$phpThumb->setParameter('h', $height);
	if( $watermark == true ) {
		$phpThumb->setParameter('fltr', 'wmi|' . CONFIG_PATH . CONFIG_DIR_IMAGES . 'watermark.jpg');
	}
	
	// set options (see phpThumb.config.php)
	// here you must preface each option with "config_"
	$phpThumb->config_output_format = 'jpeg';
	
	// Set error handling (optional)
	$phpThumb->config_error_die_on_error = false;
	
	// generate & output thumbnail
	if ($phpThumb->GenerateThumbnail()) {
		$phpThumb->RenderToFile($filename);
		// or $phpThumb->OutputThumbnail();
	} else {
		// do something with debug/error messages
		//echo 'Failed: ' . implode("\n", $phpThumb->debugmessages);
	}
}

function func_thumb_copy($src, $dest) {
	if( file_exists($src) ) {
		@copy($src, $dest);
	}
}

function func_thumb_generate_product_thumb($src, $width = 100, $height = 100) {
	//
}

function func_thumb_label_rotate_thumb($sourceFilename, $filename = '' , $width = 800, $height = 1400) {
	if( $sourceFilename == '' ) {
		return false;
	}
	if( $filename == '' ) {
		$filename = $sourceFilename;
	}
	
	//
	if( !is_dir(dirname($filename)) ) {
		if( !mkdir(dirname($filename), 0777) ) {
			return false;
		}
	}
	
	// create new phpThumb() object
	$phpThumb = new phpThumb();
	
	// set data
	// $phpThumb->rawImageData = $binary_image_data;
	$phpThumb->src = $sourceFilename;
	
	//rotate angle
	$phpThumb->ra = 270;
	
	// set parameters (see "URL Parameters" below)
	$phpThumb->w = $width;
	$phpThumb->h = $height;
	$phpThumb->q = 100;
	
	//$phpThumb->aoe = true;
	$phpThumb->aoe = ($aoe == true ? true : false);
	
	////
	//$phpThumb->setSourceFilename($sourceFilename);
	//$phpThumb->setParameter('w', $width);
	//$phpThumb->setParameter('h', $height);
	if( $watermark == true ) {
		$phpThumb->setParameter('fltr', 'wmi|' . CONFIG_PATH . CONFIG_DIR_IMAGES . 'watermark.jpg');
	}
	
	// set options (see phpThumb.config.php)
	// here you must preface each option with "config_"
	$phpThumb->config_output_format = 'jpg';
	
	// Set error handling (optional)
	$phpThumb->config_error_die_on_error = false;
	
	// generate & output thumbnail
	if ($phpThumb->GenerateThumbnail()) {
		$phpThumb->RenderToFile($filename);
		// or $phpThumb->OutputThumbnail();
		
		return true;
	} else {
		// do something with debug/error messages
		//echo 'Failed: ' . implode("\n", $phpThumb->debugmessages);
		
		return false;
	}
}

//
function func_thumb_generate_cropimage($sourceFilename, $filename = '' , $x = -1, $y = -1, $width = 300, $height = 300, 
									   $zc = 1, $aoe = true, $watermark = false) {
	if( $sourceFilename == '' ) {
		return false;
	}
	if( $filename == '' ) {
		$filename = $sourceFilename;
	}
	
	//
	if( !is_dir(dirname($filename)) ) {
		if( !mkdir(dirname($filename), 0777) ) {
			return false;
		}
	}
	
	//crop
	$sx = $sy = $sw = $sh = 0;
	
	//source size
	$swidth = $sheight = 0;
	$size = @getimagesize($sourceFilename);
	if( $size ) {
		$swidth = $size['0'];
		$sheight = $size['1'];
	}
	
	//
	if( $swidth > 0 && $sheight > 0 ) {
		//x
		if( $x == -1 ) {
			if( $swidth > $width ) {
				$sx = intval(($swidth - $width)/2);
				$sw = $width;
			} else {
				$sx = 0;
				$sw = $width;
			}
		} else {
			if( $swidth > $width + $x ) {
				$sx = $x;
				$sw = $width;
			} else {
				if( $x > $swidth ) {
					$sx = 0;
					$sw = min($swidth, $width);
				} else {
					$sx = $x;
					$sw = min($swidth - $x, $width);
				}
			}
		}
		//y
		if( $y == -1 ) {
			if( $sheight > $height ) {
				$sy = intval(($sheight - $height)/2);
				$sh = $height;
			} else {
				$sy = 0;
				$sh = $height;
			}
		} else {
			if( $sheight > $height + $y ) {
				$sy = $y;
				$sh = $height;
			} else {
				if( $y > $sheight ) {
					$sy = 0;
					$sh = min($sheight, $height);
				} else {
					$sy = $y;
					$sh = min($sheight - $y, $height);
				}
			}
		}
	} else {
		return false;
	}
	
	// create new phpThumb() object
	$phpThumb = new phpThumb();
	
	// set data
	// $phpThumb->rawImageData = $binary_image_data;
	$phpThumb->src = $sourceFilename;
	
	// set parameters (see "URL Parameters" below)
	$phpThumb->w = $width;
	$phpThumb->h = $height;
	//
	//$phpThumb->wp = $width;
	//$phpThumb->hp = $height;
	//$phpThumb->wl = $width;
	//$phpThumb->hl = $height;
	//$phpThumb->ws = $width;
	//$phpThumb->hs = $height;
	
	$phpThumb->sx = $sx;
	$phpThumb->sy = $sy;
	$phpThumb->sw = $sw;
	$phpThumb->sh = $sh;
	
	$phpThumb->q = 100;
	//$phpThumb->bc = '880000';
	//$phpThumb->bg = 'FFFFFF';
	
	$phpThumb->zc = ($zc > 0) ? 1 : 0;
	
	//$phpThumb->aoe = true;
	$phpThumb->aoe = ($aoe == true ? true : false);
	
	////
	//$phpThumb->setSourceFilename($sourceFilename);
	//$phpThumb->setParameter('w', $width);
	//$phpThumb->setParameter('h', $height);
	if( $watermark == true ) {
		$phpThumb->setParameter('fltr', 'wmi|' . CONFIG_PATH . CONFIG_DIR_IMAGES . 'watermark.jpg');
	}
	
	// set options (see phpThumb.config.php)
	// here you must preface each option with "config_"
	$phpThumb->config_output_format = 'jpeg';
	
	// Set error handling (optional)
	$phpThumb->config_error_die_on_error = false;
	
	// generate & output thumbnail
	if ($phpThumb->GenerateThumbnail()) {
		$phpThumb->RenderToFile($filename);
		// or $phpThumb->OutputThumbnail();
	} else {
		// do something with debug/error messages
		//echo 'Failed: ' . implode("\n", $phpThumb->debugmessages);
	}
}
//
?>